package com.hksj.Intelligentmanagement.aspect;

import com.hksj.Intelligentmanagement.config.WebSocket;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * WebSocket发消息切面类
 */
@Aspect
@Component
@Slf4j
public class BigScreenWebSocketMessageAOP {


    /**
     * 指定切入点
     */
    @Pointcut("@annotation(com.hksj.Intelligentmanagement.aspect.BigScreenWebSocketMessage)")
    public void pt() {
    }

    /**
     * 定义advice
     * afterReturning当返回结果时调用
     */
    @AfterReturning(pointcut = "pt()", returning = "value")
    public void sendWebSocketMessage(JoinPoint joinPoint, Object value) {
        //先通过joinPoint拿到签名
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //再通过方法签名拿到WorkOrderScreenMessage注解的message字段的值
        BigScreenWebSocketMessage annotation = methodSignature.getMethod().getAnnotation(BigScreenWebSocketMessage.class);
        String[] messages = annotation.message();

        //然后通过WebSocket给前端发送消息
        for (String message : messages) {
            WebSocket.sendMessage(message);
        }
    }

}
